<div class="layui-fluid layui-anim website-anim" id="template-update" lay-title="菜单管理">
    <div class="layui-row layui-col-space8 website-container">
        <div class="layui-col-md6 layui-col-sm6 layui-col-xs12">
            <div class="layui-card">
                <div class="layui-card-body website-table-full">
                    <form class="layui-form layui-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md8 layui-col-sm9 layui-col-xs9">
                            </div>
                            <div class="layui-col-md4 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-green-plain  table-action" id="reset">
                                    <i class="layui-icon">&#xe79b;</i>
                                </div>
                            </div>
                        </div>
                    </form>
                    <div class="eleTree menuTree" lay-filter="menuTree" style="margin-left: 1rem"></div>
                </div>
            </div>
        </div>
        <div class="layui-col-md6 layui-col-sm6 layui-col-xs12">
            <div class="layui-card">
                <div class="layui-card-header">修改模板</div>
                <div class="layui-card-body website-table-full">
                    <form class="layui-form layui-table-form" action="" lay-filter="template-update-form">
                        <div class="layui-form-item website-hide">
                            <label class="layui-form-label">主键ID：</label>
                            <div class="layui-input-block">
                                <input type="text" name="templateId" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">目录ID：</label>
                            <div class="layui-input-block">
                                <input type="text"name="menuId" lay-verify="required" readonly class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label website-form-item-require">名称：</label>
                            <div class="layui-input-block">
                                <input type="text" name="menuName" lay-verify="required" readonly autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label website-form-item-require">模板名称：</label>
                            <div class="layui-input-block">
                                <input type="text" name="templateName" lay-verify="required" autocomplete="off" class="layui-input"
                                       maxlength="50">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <input type="hidden" id="fileId" name="fileId" lay-verify="required" >
                            <label class="layui-form-label website-form-item-require">模板：</label>
                            <div class="layui-input-block">
                                <a type="text" disabled id="fileName" class="layui-input"></a>
                            </div>
                            <button type="button" class="layui-btn" id="upload-file">
                                <i class="layui-icon">&#xe67c;</i>上传模板
                            </button>
                        </div>
                        <button class="layui-btn website-hide" lay-submit="" lay-filter="template-update-form-submit" id="submit"></button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script data-th-inline="javascript" type="text/javascript">
    layui.use(['jquery', 'website', 'form', 'eleTree', 'validate','upload'], function () {
        var $ = layui.jquery,
            website = layui.website,
            form = layui.form,
            validate = layui.validate,
            eleTree = layui.eleTree,
            template = [[${template}]],
            $view = $('#template-update'),
            $reset = $view.find('#reset'),
            $submit = $view.find('#submit'),
            uploadFile,
            upload = layui.upload,
            _currentMenuData,
            _menuTree;
        var defaultChecked =[];
        //目录选中时候的集合
        var selectArry= [];
        defaultChecked.push(template.menuId);
        selectArry.push(template.menuId)
        initTemplateValue();

        form.verify(validate);
        form.render();

        _menuTree = renderMenuTree();


        eleTree.on("nodeChecked(menuTree)", function (d) {
            //清空之前勾选所选的集合
            _menuTree.unCheckArrNodes(selectArry);
            //清空对象数组
            selectArry =[];
            var data = d.data.currentData.data;
            //把当前所选的数组放入集合记录
            if(d.isChecked){
                selectArry.push(data.menuId);
            }
            _currentMenuData = data;
            form.val("template-update-form", {
                "menuName": data.menuName,
                "menuId": data.menuId
            });
        });
        $reset.on('click', function () {
            defaultChecked.push(template.menuId);
            selectArry.push(template.menuId);
            reloadMenuTree();
            initTemplateValue();
        });
        $submit.on('click', function () {
            $view.find('#submit-form').trigger('click');
        });
        uploadFile = upload.render({
            elem: '#upload-file' //绑定元素
            , url: '/upload' //上传接口
            , done: function (res) {
                if (res.code === 200) {
                    var data = res.data
                    $('#fileName')[0].setAttribute('href', ctx + 'download/' + data.fileId)
                    $("#fileName").text(data.name)

                    $('#fileId').val(data.fileId);
                    website.alert.success('上传文件成功');
                }
            }
            , error: function () {
                //请求异常回调
                website.alert.error('上传文件失败');
            },
            accept: 'file',
            size: 1024 * 50
        });


        function renderMenuTree() {
            _menuTree = eleTree.render({
                elem: '.menuTree',
                url: ctx + 'menu/tree',
                where: {
                    "type":0,
                    "invalidate_ie_cache": new Date()
                },
                accordion: true,
                defaultCheckedKeys:defaultChecked,
                highlightCurrent: true,
                showCheckbox: true,
                checkStrictly: true,
                renderAfterExpand: false,
                request: {
                    name: "title",
                    key: "id",
                    children: "childs",
                    checked: "checked",
                    data: "data"
                },
                response: {
                    statusName: "code",
                    statusCode: 200,
                    dataName: "data"
                }
            });
            return _menuTree;
        }
        function initTemplateValue() {
            if(template.fileName!=null && template.fileName!=''){
                $('#fileName')[0].setAttribute('href', ctx + 'download/' + template.fileId)
                $("#fileName").text(template.fileName)
            }
            form.val("template-update-form", {
                "templateId": template.templateId,
                "menuId": template.menuId,
                "menuName": template.menuName,
                "templateName": template.templateName,
                "fileId": template.fileId
            });

        }
        function reloadMenuTree() {
            _menuTree = renderMenuTree();
        }
        form.on('submit(template-update-form-submit)', function (data) {
            website.post(ctx + 'template/update', data.field, function () {
                layer.closeAll();
                website.alert.success('修改成功');
                $('#website-template').find('#query').click();
            })
            return false;
        });
    });
</script>